package com.ruoyi.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.IdVo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.RoleVo;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.user.domain.TbPerm;
import com.ruoyi.user.domain.TbRole;
import com.ruoyi.user.service.TbRoleService;
import com.ruoyi.user.mapper.TbRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class TbRoleServiceImpl extends ServiceImpl<TbRoleMapper, TbRole>
    implements TbRoleService{

    @Autowired
    private TbRoleMapper tbRoleMapper;

    @Override
    public R addRole(RoleVo roleVo) {

        TbRole tbRole = new TbRole();
        BeanUtils.copyProperties(roleVo, tbRole);
        tbRole.setUpdateBy("admin");
        tbRole.setCreateBy("admin");
        int insert = tbRoleMapper.insert(tbRole);

        return insert > 0 ? R.ok("添加成功") : R.fail("添加失败");
    }

    @Override
    public R deleteRoleById(IdVo idVo) {
        int delete = tbRoleMapper.deleteById(idVo.getId());

        return delete > 0 ? R.ok("删除成功") : R.fail("删除失败");
    }

    @Override
    public AjaxResult selectRole() {
        List<TbRole> tbRoles = tbRoleMapper.selectList(null);

        return AjaxResult.success("查询可能为空",tbRoles);
    }

    @Override
    public R updateRole(RoleVo roleVo) {
        TbRole tbRole = new TbRole();
        BeanUtils.copyProperties(roleVo, tbRole);
        tbRole.setUpdateBy("admin");
        tbRole.setUpdateTime(null);

        int update = tbRoleMapper.updateById(tbRole);
        return update > 0 ? R.ok("修改成功") : R.fail("修改失败");
    }
}




